package com.base.pig.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.pig.dto.ScLayerDto;
import com.base.pig.dto.ScLayerTypeDto;
import com.base.pig.entity.ScLayer;
import com.base.pig.mapper.ScLayerMapper;
import com.base.pig.service.ScLayerService;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ProjectName intelligent-city
 * @Package com.pig4cloud.pig.lab.service.impl
 * @ClassName ScLayerServiceImpl
 * @Description TODO
 * @Company Lab
 * @Author ZhangZhY
 * @Date 2020/6/23 10:43
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Service
@Slf4j
public class ScLayerServiceImpl extends ServiceImpl<ScLayerMapper, ScLayer> implements ScLayerService {
    @Autowired
    ScLayerMapper scLayerMapper;
    /***
     * @Title queryLength
     * @Description 获取管线图层长度
     * @return String
     *
     */
    @Override
    public String queryLength(String ids) {
        if(null == scLayerMapper.queryLength(ids.split(","))){
            return "0";
        }
        return scLayerMapper.queryLength(ids.split(","));
    }

    /**
     * @Title allTree
     * @Description  所有图层tree
     * @return java.util.List<com.pig4cloud.pig.lab.dto.ScLayerDto>
     * @author zhangzhy
     * @date 2020/7/10 14:35
     */
    @Override
    public Map<String,Object> allTree() {
        List<ScLayer> listScLayerTwoDimension = this.list(Wrappers.<ScLayer>query().ne("type",3).ne("type",2).ne("type",4).ne("type",7).ne("type",8));
        List<ScLayer> listScLayerThreeDimension = this.list(Wrappers.<ScLayer>query().ne("type",0).ne("type",1).ne("type",4).ne("type",5).ne("type",6));
        Map<String,Object> map =  new HashMap<>(0);



        //magang  20231017  先只改二维小区 ，后续其他可能也会改
        List<ScLayerDto>  listTwoDimension=new ArrayList<>();

        ScLayerDto  TwoDimension=new  ScLayerDto();
        TwoDimension.setKey((long) 0);
        TwoDimension.setId((long) 0);
        TwoDimension.setTitle("全选");
        TwoDimension.setSeq(1);
        TwoDimension.setName("全选");
        TwoDimension.setLabel("全选");
        TwoDimension.setChildren(getDimensionList(listScLayerTwoDimension));
        listTwoDimension.add(TwoDimension);

        List<ScLayerDto>  listThreeDimension=new ArrayList<>();

        ScLayerDto  ThreeDimension=new  ScLayerDto();
        ThreeDimension.setKey((long) 1);
        ThreeDimension.setId((long) 1);
        ThreeDimension.setTitle("全选");
        ThreeDimension.setSeq(2);
        ThreeDimension.setName("全选");
        ThreeDimension.setLabel("全选");
        ThreeDimension.setChildren(getDimensionList(listScLayerThreeDimension));

        listThreeDimension.add(ThreeDimension);
        map.put("TwoDimension",listTwoDimension);
        map.put("ThreeDimension",listThreeDimension);
        return map ;
    }

    private List<ScLayerDto> getDimensionList(List<ScLayer> listScLayerDimension){
        List<List<ScLayerDto>> listDimension = listScLayerDimension.stream().filter(scLayer -> scLayer.getPid() == -1L).map(
                scLayer -> {
                    return getChildrens(scLayer, listScLayerDimension);
                }
        ).collect(Collectors.toList());
        return listDimension.get(0);
    }
    /***
     * @Title getChildrens
     * @Description  遍历所有子级
     * @param scLayer :
     * @param all :
     * @return java.util.List<com.pig4cloud.pig.lab.dto.ScLayerDto>
     * @author zhangzhy
     * @date 2020/6/24 11:47
     */
    private List<ScLayerDto> getChildrens(ScLayer scLayer, List<ScLayer> all) {
        List<ScLayerDto> childrens = all.stream().filter(m -> {
            return Objects.equals(m.getPid(), scLayer.getId());
        }).map(m -> {
            ScLayerDto scLayerDto = new ScLayerDto();
            scLayerDto.setName(m.getName());
            scLayerDto.setLabel(m.getLabel());
            scLayerDto.setId(m.getId());
            scLayerDto.setSeq(m.getSeq());
            //magang  20220121  添加key和title
            scLayerDto.setTitle(m.getLabel());
            scLayerDto.setKey(m.getId());

            scLayerDto.setChildren(getChildrens(m, all));
            return scLayerDto;
        }).sorted(Comparator.comparingInt(ScLayerDto::getSeq)).collect(Collectors.toList());
        return childrens;
    }

    /**
     * @Title typeTree
     * @Description  图层中文对照接口
     * @return java.util.List<com.pig4cloud.pig.lab.dto.ScLayerTypeDto>
     * @author zhangzhy
     * @date 2020/7/10 14:33
     */
    @Override
    public List<ScLayerTypeDto> typeTree() {
        List<ScLayer> layers = this.list(Wrappers.<ScLayer>query().eq("type",0));
        List<ScLayerTypeDto> scLayerTypeDtoList = new ArrayList<>();
        layers.forEach(scLayer -> {
            ScLayerTypeDto scLayerTypeDto = new ScLayerTypeDto();
            scLayerTypeDto.setName(scLayer.getName());
            scLayerTypeDto.setLabel(scLayer.getLabel());
            scLayerTypeDtoList.add(scLayerTypeDto);
        });
        return scLayerTypeDtoList;
    }

    @Override
    public Map<String, List<ScLayerDto>> constructionTree() {
        List<ScLayer> scLayerList = this.list(Wrappers.<ScLayer>query().in("type",-1,4));
        Map<String,List<ScLayerDto>> map =  new HashMap<>(0);
        map.put("TwoDimension",new ArrayList<>());
        map.put("ThreeDimension",getDimensionList(scLayerList));
        return map;
    }
}
